package com.example.myapp.Activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.myapp.ADO.ActivityManager;
import com.example.myapp.ADO.MyDialog;
import com.example.myapp.ADO.UpdateAppManager;
import com.example.myapp.R;

import cn.bmob.v3.BmobUser;

public class Setting extends AppCompatActivity {

    public void onBackPressed() {
        //此处写退向后台的处理
        finish();
    }//手机返回键

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ActivityManager.getInstance().addActivity(this);
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);  //隐藏标题栏
        setContentView(R.layout.setting);
        LinearLayout bt0=findViewById(R.id.setting_btn0);
        LinearLayout bt1=findViewById(R.id.setting_bt1);
        LinearLayout bt2=findViewById(R.id.setting_bt2);
        TextView bt3=findViewById(R.id.setting_btn3);
        LinearLayout setting_updataapp=findViewById(R.id.setting_updataapp);

        LinearLayout setting_uppwd=findViewById(R.id.setting_uppwd);

        ImageView setting_back=findViewById(R.id.setting_back);
        setting_updataapp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UpdateAppManager manager=new     UpdateAppManager(Setting.this);
                manager.  getappVsionMsg();
            }
        });

        setting_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        bt0.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!BmobUser.isLogin()){
                    Toast.makeText(getApplicationContext(), "请登陆后再操作！", Toast.LENGTH_SHORT).show();
                    return;
                }
                startActivity(new Intent(Setting.this, EditUser.class));

            }
        });
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(Setting.this, Notice.class);
                intent.putExtra("title", "用户协议");//打开主页
                intent.putExtra("path", "file:///android_asset/yonghuxieyi.html");//打开主页
                startActivity(intent);
            }
        });
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(Setting.this, Notice.class);
                intent.putExtra("title", "隐私政策");//打开主页
                intent.putExtra("path", "file:///android_asset/yinsizhengce.html");//打开主页
                startActivity(intent);
            }
        });
        bt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //此处写退向后台的处理
                if(!BmobUser.isLogin()){
                    Toast.makeText(getApplicationContext(), "请登陆后再操作！", Toast.LENGTH_SHORT).show();
                    return;
                }
                Log.w("aaaa","我点击返回键！");
                View view = getLayoutInflater().inflate(R.layout.exit_dialog_layout, null);
                MyDialog mMyDialog = new MyDialog(Setting.this, 0, 0, view, R.style.DialogTheme);
                TextView exitall=  view.findViewById(R.id.exitall);
                TextView dialog_title= view.findViewById(R.id.dialog_title);
                TextView exitback=  view.findViewById(R.id.exitback);
                dialog_title.setText("确定退出登录吗？");
                mMyDialog.setCancelable(true);
                mMyDialog.show();
                exitall.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        BmobUser.logOut();
                        SharedPreferences sp = getSharedPreferences("login", Context.MODE_PRIVATE);
                        //获取editor
                        SharedPreferences.Editor editor = sp.edit();
                        //构造数据对
                        editor.putString("pwd","");
                        editor.putBoolean("save", true);
                        //提交数据
                        editor.commit();
                        //数据将保存到文件里
                        mMyDialog.dismiss();
                        Intent intent = new Intent(Setting.this, Login.class);
                        startActivity(intent);
                    }
                });//退出程序
                exitback.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mMyDialog.dismiss();
                    }
                });//退出程序


            }
        });
        setting_uppwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!BmobUser.isLogin()){
                    Toast.makeText(getApplicationContext(), "请登陆后再操作！", Toast.LENGTH_SHORT).show();
                    return;
                }
                startActivity(new Intent(Setting.this, UPpwd.class));
            }
        });

    }
}
